<template>
  <div class="audit-container">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="userId" label="用户ID" width="180" />
      <el-table-column prop="username" label="用户名" width="180" />
      <el-table-column prop="realName" label="真实姓名" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column prop="createdAt" label="创建时间" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleApprove(scope.row.userId)" style="margin-right: 10px">审核</el-button>
          <el-button type="danger" size="small" @click="handleRevoke(scope.row.userId)">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const tableData = ref([{
  userId: '',
  username: '',
  realName: '',
  phone: '',
  createdAt: ''
}])

const fetchAdminList = async () => {
  try {
    const response = await axios.get("/api/admin/passAdmin")
    tableData.value = response.data.data
    
  } catch (error) {
    console.error('获取管理员列表失败:', error)
    tableData.value = []
  }
}

const handleApprove = async (userId) => {
  try {
    await axios.get(`/api/admin/passAdmin/pass?userId=${userId}`)
    await fetchAdminList()
  } catch (error) {
    console.error('审核管理员失败:', error)
  }
}

const handleRevoke = async (userId) => {
  try {
    await axios.get(`/api/admin/passAdmin/del?userId=${userId}`)
    await fetchAdminList()
  } catch (error) {
    console.error('撤销管理员失败:', error)
  }
}

onMounted(() => {
  fetchAdminList()
})
</script>

<style scoped>
.audit-container {
  padding: 20px;
}
</style>